<template>
  <div class="app-container flex-col minH">
    <div class="topline w100p bgwhite pt20 pb10 radius10 pl10 pr20 justify-between">
      <div>
        <el-input v-model="uid" style="width: 280px" placeholder="搜尋經紀人用戶資訊..." :prefix-icon="Search" class="search mr10 mb10" />
        <el-input v-model="level" style="width: 280px" placeholder="搜尋經紀人等級..." :prefix-icon="Search" class="search mb10" />

      </div>
      <div class="noshrink mb10">
        <el-button class="btn2" @click="search()">查詢</el-button>
        <el-button class="btn1" @click="reset()">重置</el-button>
      </div>
    </div>
    <div class="mt15 radius10 hidden pb15 flex1 bgwhite  flex-col w100p">
      <div class="flex1 w100p ">
        <el-table :data="borkerListTable" style="width: 100%" cell-class-name="tbcell" header-cell-class-name="hdcell">

          <el-table-column prop="" label="經紀人用戶訊息" min-width="200">
            <template #default="scope">
              <span class="mr5" @click="showUser(scope.row.user.id)">{{ scope.row.user.name }}(ID:{{ scope.row.user.id }})</span>
            </template>
          </el-table-column>
          <el-table-column prop="level" label="經紀人等級" />
          <el-table-column prop="num" label="經紀人團隊人數" sortable>
            <template #default="scope">
              <div class="align-center" @click="dialogShow(1, scope.row)">
                <span class="mr5">{{ scope.row.num }}</span>
                <el-icon>
                  <View />
                </el-icon>
              </div>
            </template>
          </el-table-column>
          <el-table-column prop="performance" label="經紀人團隊業績" sortable>
            <template #default="scope">
              <div class="align-center" @click="dialogShow(2, scope.row)">
                <span class="mr5">{{ scope.row.performance }}</span>
                <el-icon>
                  <View />
                </el-icon>
              </div>
            </template>
          </el-table-column>
          <el-table-column prop="created_at" label="創建時間" />

        </el-table>
      </div>
      <div class=" mlauto plr20  ">
        <el-pagination v-model:current-page="currentPage1" prev-text="上一頁" next-text="下一頁" :default-page-size="15" :small="small" :disabled="disabled" :background="true" layout="prev, pager, next, jumper" :total="total1" @current-change="handleCurrentChange1" />
      </div>

    </div>

    <el-dialog v-model="dialogTableVisible" :title="dialogType == 1 ? '團隊人數詳情' : '團隊業績詳情'" width="1000" @close="onClose">

      <div class="plr20 justify-between ptb20">
        <div>
          <el-date-picker v-model="start" type="datetime" placeholder="开始时间" value-format="YYYY-MM-DD HH:mm:ss" />
          <span class="plr10">-</span>
          <el-date-picker v-model="end" type="datetime" placeholder="结束时间" value-format="YYYY-MM-DD HH:mm:ss" />
          <!-- <el-select v-model="value" placeholder="時間篩選" size="large" style="width: 240px">
            <el-option v-for="item in options" :key="item.value" :label="item.label" :value="item.value" />
          </el-select> -->
        </div>
        <div class="noshrink">
          <el-button class="btn2" @click="dialogSearch()">查詢</el-button>
          <el-button class="btn1" @click="dialogReset()">重置</el-button>
        </div>

      </div>
      <!-- 团队人数 -->
      <template v-if="dialogType == 1">
        <div class="">
          <el-table :data="teamListTable" cell-class-name="dialogtbcell" header-cell-class-name="dialoghdcell">
            <el-table-column property="" label="用戶訊息">
              <template #default="scope">
                <span class="mr5">{{ scope.row.phone }}(ID:{{ scope.row.id }})</span>
              </template>
            </el-table-column>

            <el-table-column property="name" label="用戶昵稱" />
            <el-table-column property="avatar" label="用戶頭像" width="200">
              <template #default="scope">
                <el-avatar shape="square" :size="50" :fit="'cover'" :src="scope.row.avatar" />
              </template>

            </el-table-column>
            <el-table-column property="created_at" label="邀請時間" />

          </el-table>
          <div class="  plr20 justify-between mt20">
            <div style="flex:1;" class="clr3c4 bold">總共{{ total2 }}條</div>
            <el-pagination v-model:current-page="currentPage2" prev-text="上一頁" next-text="下一頁" :default-page-size="15" :small="small" :disabled="disabled" :background="true" layout="prev, pager, next, jumper" :total="total2" @current-change="handleCurrentChange2" />
          </div>
        </div>

      </template>
      <!-- 团队业绩 -->
      <template v-if="dialogType == 2">
        <div class="">
          <el-table :data="performanceTable" cell-class-name="dialogtbcell" header-cell-class-name="dialoghdcell">
            <el-table-column property="id" label="序號" width="100" />
            <el-table-column property="fromid" label="收入對象" />
            <el-table-column property="action_text" label="收入類型">

            </el-table-column>
            <el-table-column property="money" label="收入金額" />

          </el-table>
          <div class=" plr20 justify-between mt20">
            <div class="clr3c4 bold noshrink">總共{{ total3 }}條</div>
            <div>
              <div>小計(元): <span class="ft46 clr3c4 ml10">{{ all_money }}</span> </div>
            </div>
          </div>
          <div class="flex-col w100p">
            <div class="plr20 mt20 mlauto">
              <el-pagination v-model:current-page="currentPage3" prev-text="上一頁" next-text="下一頁" :default-page-size="15" :small="small" :disabled="disabled" :background="true" layout="prev, pager, next, jumper" :total="total3" @current-change="handleCurrentChange3" />
            </div>
          </div>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import { Search } from '@element-plus/icons-vue'
import { ref, onMounted } from 'vue'
import { useRouter, } from 'vue-router';
import { brokerList, teamList, performance } from '@/api/brokers'
import { inject } from 'vue';
const showUser = inject('showUser');
onMounted(() => {
  getBrokerList()
})
const getBrokerList = () => {
  brokerList({
    page: currentPage1.value,
    uid: uid.value,
    level: level.value
  }).then((res) => {
    console.log('等级列表', res)
    borkerListTable.value = res.data.data
    total2.value = res.data.total
  })
}
// 团队人数详情
const getTeamList = () => {
  teamList({
    pid: currentId.value,
    page: currentPage2.value,
    start: start.value,
    end: end.value
  }).then((res) => {
    console.log('团队人数', res)
    teamListTable.value = res.data.data
    total2.value = res.data.total

  })
}
// 团队业绩详情
const getPerformanceList = () => {
  performance({
    pid: currentId.value,
    page: currentPage3.value,
    start: start.value,
    end: end.value
  }).then((res) => {
    console.log('团队人数', res)
    performanceTable.value = res.data.data
    all_money.value = res.total
    total3.value = res.data.total

  })
}
const all_money = ref('')
const currentId = ref('')
const uid = ref('')
const level = ref('')
const dialogTableVisible = ref(false)
const dialogType = ref(1)
const dialogShow = (type, row) => {
  console.log('dialog----show----')
  dialogType.value = type
  dialogTableVisible.value = true
  // 团队人数详情
  if (type == 1) {
    currentId.value = row.uid
    getTeamList()
  }
  if (type == 2) {
    // all_money.value = row.performance
    currentId.value = row.uid
    getPerformanceList()
  }
}
const start = ref('')
const end = ref('')
const onClose = () => {
  start.value = ''
  end.value = ''
}
// 弹窗搜尋
const dialogSearch = () => {
  if (dialogType.value == 1) {
    getTeamList()
  }
  if (dialogType.value == 2) {
    getPerformanceList()
  }

}
// 弹窗重置
const dialogReset = () => {
  start.value = ''
  end.value = ''
  if (dialogType.value == 1) {
    getTeamList()
  }
  if (dialogType.value == 2) {
    getPerformanceList()
  }

}
// 搜尋
const search = () => {
  getBrokerList()
}
// 重置
const reset = () => {
  uid.value = ''
  level.value = ''
  getBrokerList()

}
const borkerListTable = ref([]) //经纪人列表数据
const teamListTable = ref([]) //团队人数列表数据
const performanceTable = ref([]) //业绩列表数据



const tableRowClassName = () => {
  return 'tbrow'
}
const total1 = ref(0)
const currentPage1 = ref(1)
const handleCurrentChange1 = (val) => {
  console.log(`current page: ${val}`)
  getBrokerList()
}
const total2 = ref(0)
const currentPage2 = ref(1)
const handleCurrentChange2 = (val) => {
  console.log(`current page: ${val}`)
  getTeamList()
}
const total3 = ref(0)
const currentPage3 = ref(1)
const handleCurrentChange3 = (val) => {
  console.log(`current page: ${val}`)
  getPerformanceList()
}
const router = useRouter()
const toAdd = () => {
  console.log(router)
  router.push({ path: 'addNotice' })
}
const edit = (type) => {
  router.push({ path: 'signEdit' })
}
</script>

<style lang="scss" scoped>
.search {
  overflow: hidden;
  border-radius: 10px !important;
  // padding: 5px 0px;
}
</style>
<style></style>
 